home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / Bricks demo ƒ / Bricks.p < prev    next >
Text File  |  1995-04-23  |  7KB  |  238 lines

  1. {Bricks}
  2. {}
  3. {Demo program for SAT 2.3, by Ingemar Ragnemalm november 1994}
  4. {}
  5. {The purpose of this demo is to test and demonstrate the support for what I call "resting" sprites.}
  6. {If a sprite neither moves, changes face, overlaps a changing sprite nor changes its place in the}
  7. {sprite list, then SAT can avoid drawing it.}
  8. {}
  9. {For programs where all sprites change in every frame (which is the case in many arcade games)}
  10. {then this is not of any interest at all, and will just slow things down. For other programs, for}
  11. {example board games, Tetris-style games etc, this technique is very useful.}
  12. {}
  13. {For the moment (SAT 2.3d3), you get the new system by calling SATRun2 instead of SATRun. The name}
  14. {of SATRun2 is likely to change in the future.}
  15. {}
  16. {When you start Bricks, the sprites will not be initialied in order, so since VPositionSort}
  17. {is active, they will sort during the first frames. This slows the program down for the first}
  18. {few seconds. If this is a problem, it can be avoided by either turning off sorting or}
  19. {creating the sprite in the proper places from the start.}
  20. {}
  21. {Bug note: When using "fast graphics", the cursor will cause some "mouse droppings". This is best avoided}
  22. {by hiding the cursor and repllacing it with a SAT sprite. The "proper" way to avoid the problem is to}
  23. {call ShieldCursor, but that will make the cursor flicker a lot.}
  24.  
  25. program Bricks;
  26.     uses
  27. {$ifc UNDEFINED THINK_PASCAL}
  28.         Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps,{}
  29.         Memory, SegLoad, Scrap, ToolUtils, OSUtils, Menus, Resources, StandardFile,{}
  30.         GestaltEqu, Files, Errors, Devices, 
  31. {$elsec}
  32.         InterfacesUI, 
  33. {$endc}
  34.         SAT;
  35.  
  36.     const
  37.         kNumBricks = 30;
  38.     var
  39.         brickFace: array[0..kNumBricks] of FacePtr;
  40.         myEvent: EventRecord;
  41.         whichWindow: WindowPtr;
  42.         appleMenu, fileMenu: MenuHandle;
  43.         gUseStagger, gDone, gUseFast, gAllowBackground: Boolean;
  44.         i: integer;
  45.         sp, found: SpritePtr;
  46.         theSelection: Longint;
  47.         where: Point;
  48.         theKey: Char;
  49.         whichPart: Integer;
  50.         hasEvent: Boolean;
  51.  
  52.     procedure InitBricks;
  53.         var
  54.             i: integer;
  55.     begin
  56.         for i := 0 to kNumBricks - 1 do
  57.             brickFace[i] := SATGetFace(i + 128);
  58.     end;
  59.  
  60.     procedure HandleRestingBrick (me: SpritePtr);
  61.     begin
  62.     end;
  63.  
  64.     procedure HandleFollowMouse (me: SpritePtr);
  65.         var
  66.             mousePos: Point;
  67.     begin
  68.         if Button then
  69.             begin
  70.                 GetMouse(mousePos);
  71.                 me^.position.h := me^.position.h + mousePos.h - me^.speed.h;
  72.                 me^.position.v := me^.position.v + mousePos.v - me^.speed.v;
  73.                 me^.speed := mousePos;
  74.             end
  75.         else
  76.             me^.task := @HandleRestingBrick;
  77.     end;
  78.  
  79.     procedure SetupBrick (me: SpritePtr);
  80.     begin
  81.         me^.task := @HandleRestingBrick;
  82.         me^.face := brickFace[me^.kind];
  83.     end;
  84.  
  85.     procedure SynchMenus;
  86.     begin
  87.         CheckItem(fileMenu, 1, not gUseStagger);
  88.         CheckItem(fileMenu, 2, gUseStagger);
  89.         CheckItem(fileMenu, 3, gUseFast);
  90.         CheckItem(fileMenu, 4, gAllowBackground);
  91.     end;
  92.  
  93.     procedure MenuSelection (theSelection: Longint);
  94.         var
  95.             name: Str255;
  96.             saveport: GrafPtr;
  97.     begin
  98.         case HiWord(theSelection) of
  99.             128: 
  100.                 begin
  101.                     if LoWord(theSelection) = 1 then
  102.                         SATReportStr('Experimental SAT demo program. Resting sprites allow good speed with many sprites!')
  103.                     else
  104.                         begin
  105.                             GetPort(saveport);
  106.                             GetMenuItemText(appleMenu, 1, name);    (* get name *)
  107.                             if OpenDeskAcc(name) = 0 then (* run the desk accessory *)
  108.                                 ;
  109.                             SetPort(saveport);
  110.                         end;
  111.                 end;
  112.             129: 
  113.                 case LoWord(theSelection) of
  114.                     1: 
  115.                         gUseStagger := false;
  116.                     2: 
  117.                         gUseStagger := true;
  118.                     3: 
  119.                         gUseFast := not gUseFast;
  120.                     4: 
  121.                         gAllowBackground := not gAllowBackground;
  122.                     6: 
  123.                         gDone := true;
  124.                 end; {case MenuSelect}
  125.             otherwise
  126.         end;
  127.         SynchMenus;
  128.         HiLiteMenu(0);
  129.     end;
  130.  
  131. begin
  132. {CodeWarrior needs inits here}
  133. {$IFC UNDEFINED THINK_PASCAL}
  134.     SATInitToolbox;
  135. {$ENDC}
  136.  
  137.     InitBricks;
  138.  
  139.     SATInit(128, 129, 512, 342);
  140.     SATSetPortScreen;
  141.     gUseStagger := true;
  142.     gAllowBackground := true;
  143.     gUseFast := false;
  144.  
  145.     for i := 0 to kNumBricks - 1 do
  146.         sp := SATNewSprite(i, SATRand(gSAT.offSizeH - 48), SATRand(gSAT.offSizeV - 64), @SetupBrick);
  147.     for i := 0 to kNumBricks - 1 do
  148.         sp := SATNewSprite(i, SATRand(gSAT.offSizeH - 48), SATRand(gSAT.offSizeV - 64), @SetupBrick);
  149.  
  150.     appleMenu := NewMenu(128, stringof(char($14)));
  151.     AppendMenu(appleMenu, 'About Bricks demo…;(-');
  152.     AppendResMenu(appleMenu, 'DRVR');
  153.     InsertMenu(appleMenu, 0);            { put apple menu at end of menu bar }
  154.  
  155.     fileMenu := NewMenu(129, 'File');
  156.     AppendMenu(fileMenu, 'RunSAT/1;RunSAT2/2;Fast graphics/F;Allow background tasks;(-;Quit/Q');
  157.     InsertMenu(fileMenu, 0);            { put file menu at end of menu bar }
  158.     DrawMenuBar;
  159.     SynchMenus;
  160.  
  161.     InitCursor;
  162.  
  163.     repeat
  164. {if gUseFast then}
  165. {ShieldCursor(gSAT.bounds, Point(0));}
  166.         if gUseStagger then
  167.             SATRun2(gUseFast)
  168.         else
  169.             SATRun(gUseFast);
  170. {if gUseFast then}
  171. {ShowCursor;}
  172.  
  173.         if gAllowBackground then
  174.             begin
  175.                 SystemTask;
  176.                 hasEvent := GetNextEvent(everyEvent, myEvent)
  177.             end
  178.         else
  179.             hasEvent := GetOSEvent(everyEvent, myEvent);        {or perhaps mDownMask + updateMask + keyDownMask}
  180.         if hasEvent then
  181.             case myEvent.what of
  182.                 updateEvt: 
  183.                     if WindowPtr(myEvent.message) = gSAT.wind.port then
  184.                         begin
  185.                             BeginUpdate(gSAT.wind.port);
  186.                             SATRedraw;
  187.                             EndUpdate(gSAT.wind.port);
  188.                         end;
  189.                 keyDown: 
  190.                     begin
  191.                         theKey := char(BitAnd(myEvent.message, charCodeMask));
  192.                         if (BitAnd(myEvent.modifiers, cmdKey) <> 0) then
  193.                             MenuSelection(MenuKey(theKey))
  194.                         else
  195. {DoKey(theKey, theEvent.modifiers)}
  196.                             ;
  197.                     end;
  198.                 mouseDown: 
  199.                     begin
  200.                         whichPart := FindWindow(myEvent.where, whichWindow);
  201.                         case whichPart of
  202.                             inMenuBar: 
  203.                                 begin
  204.                                     theSelection := MenuSelect(myEvent.where);
  205.                                     MenuSelection(theSelection);
  206.                                 end;
  207.                             inSysWindow: 
  208.                                 SystemClick(myEvent, whichWindow);
  209.                             inContent: 
  210.                                 begin
  211.                                     found := nil;
  212.                                     sp := gSAT.sRoot;
  213.                                     GetMouse(where);
  214.                                     while sp <> nil do
  215.                                         begin
  216.                                             if PtInRect(where, sp^.r) then
  217.                                                 found := sp;
  218.                                             sp := sp^.next;
  219.                                         end;
  220.                                     if found <> nil then
  221.                                         if myEvent.modifiers <> 0 then
  222.                                             found^.task := nil
  223.                                         else
  224.                                             begin
  225.                                                 found^.task := @HandleFollowMouse;
  226.                                                 found^.speed := where; {not speed, but storage for old mouse position}
  227.                                             end;
  228.                                 end;
  229.                             otherwise
  230.                         end; {case whichPart}
  231.                     end;
  232.  
  233.                 otherwise
  234.             end;{case myEvent.what}
  235.     until gDone;
  236.  
  237.     SATSoundShutup;
  238. end.